Project 1 - Temperature metric converter
On this page
Learning objectives:
- Create variables
- Use calculations on variables
- Print out helpful message about conversion
Outcomes
We will write a program that can convert temperature data. Your code will automate the process of taking data and making it into a presentable format.
Make a Celsius to Fahrenheit temperature converter
We will write a programme that converts Celsius to Fahrenheit, and Fahrenheit to Celsius. It will then write out a helpful message.
We have two temperatures from a given time in London and New York. The London temperature is in Celsius, and the New York temperature is in Fahrenheit. In the outputs the numbers are rounded to zero decimal places. We are aiming for the following outputs:
The temperature in London is 25ยฐC, which is 77ยฐF
The temperature in New York is 80ยฐF, which is 27ยฐC
Steps to help you get to the outcome
Part 1 - the setup
Open an R script file and save it.
Part 2 - printing values
Print the value 25.
Part 3 - Celsius to Fahrenheit calculation
Transform the value of 25 Celsius to Fahrenheit with this formula:
\((Celsius \times 9/5) + 32\)
Part 4 - using variables
Store the value of the temperature in Celsius (25) and print it using the stored value.
There are some useful resources online which can help you such as W3schools.
Part 5 - calculations using variables
Use the stored Celsius value to calculate the temperature in Fahrenheit.
Store the outcome of this calculation and print it using the stored value.
Part 6 - more calculations
Transform the value of 80 Fahrenheit to Celsius with this formula:
\((Fahrenheit - 32) \times 5/9\)
As before, store the Fahrenheit value and the outcome of the calculation. Print the outcome using the stored value.
Part 7 - making a text output
Now we can bring it all together to make our final output.
Use R to make the two text outputs by concatenating elements from one or more variables into a single string.
This should be automated, so if we change the variable for Celsius or Fahrenheit the code will show the new output.
You can search online here, or use the built in help page in RStudio.
For example, the paste() function will help you here. Try running ?paste to bring up the help page in RStudio.
Part 8 - tidy up
Round the outputs to make sure we are looking at whole numbers only.
Part 9 - try different temperatures
Test out your code by converting different temperatures.
Change your temperature variables to the following and re-run your code:
- 10 Celsius as Fahrenheit
- 20 Fahrenheit as Celsius
Final task - fill out the survey!
We are always looking to improve and iterate our workshops. Follow the link to give your feedback.